// blob转base64 核心
let blobToBase64 = function(blob) {
    return new Promise(function(resolve) {
        let reader = new FileReader();
        reader.onload = function(event) {
            let b = `data:image/jpg;base64,${window.btoa(event.target.result)}`;
            resolve(b);
        }; // data url!
        reader.readAsBinaryString(blob);
    });
}

let fileToBase64 = function(file) {
    return new Promise(function(resolve) {
        let reader = new FileReader()
        reader.addEventListener("load", () => {
            // console.info('图片修改', reader.result)
            resolve(reader.result);
        }, false)
        if (file) {
            reader.readAsDataURL(file)
        }
    })
}

// 批量处理
function uploadImgBatch_blob(list) {
    let l = [];
    let arr = []

    for (let i = 0; i < list.length; i++) {
        l[i] = list[i];
        arr.push(blobToBase64(list[i].blob).then(b => {
            l[i].base64 = b;
        }));
    }

    return Promise.all(arr).then(() => {
        return l;
    })

}

function uploadImgBatch_flie(list) {
    // console.info('list', list)
    let l = [];
    let arr = []

    for (let i = 0; i < list.length; i++) {
        l[i] = list[i];
        arr.push(fileToBase64(list[i].file).then(b => {
            l[i].base64 = b;
        }));
    }

    return Promise.all(arr).then(() => {
        return l;
    })

}


export {
    blobToBase64,
    fileToBase64,
    uploadImgBatch_blob,
    uploadImgBatch_flie

}